package org.example.medicalstaff.controller;

import com.alibaba.fastjson.JSONObject;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.example.medicalstaff.entity.CheckRelation;
import org.example.medicalstaff.entity.CheckTemplate;
import org.example.medicalstaff.entity.FmedItem;
import org.example.medicalstaff.service.CheckService;
import org.example.medicalstaff.utils.DateStringFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/out_doctor")
public class CheckTemplateController {
    @Autowired
    CheckService checkService;
    /**
        新增医技模板
        测试URL:http://127.0.0.1:9090/medical_technology_template/add_templ
        备注：没什么好注意的，就时间格式要对
        测试结果：正常
     */
    @PostMapping("/medical_technology_template/add_templ")
    public CommonResponse<CheckTemplate> check_manageTemplate_add1(
            HttpServletRequest httpRequest
           ) {
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int RecordType= Integer.parseInt((String)jsonObject.get("RecordType"));
        int UsersID=(int)jsonObject.get("UsersID");
        int Scope = 1;
        //设置当前时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);
        String pattern = "yyyy-MM-dd HH:mm:ss";
        Date date = DateStringFormat.parseDateTimeString(formattedDateTime,pattern);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String CreationTime = sdf2.format(date);

        String Name =(String)jsonObject.get("Name");
        CommonResponse<CheckTemplate> commonResponse =checkService.check_manageTemplate_add(
                Name,Scope,RecordType,UsersID,CreationTime
        );
        return commonResponse;
    }
    /**
        删除医技模板
        测试URL:http://127.0.0.1:9090/medical_technology_template/del_templ/{name}
        备注：后面要带name参数,然后这里删是两个表里都删，我也不知道有没有外键约束，最好是看看两个表都有再操作避免出问题
        测试结果：正常
     */
    @GetMapping("/medical_technology_template/del_templ")
    public CommonResponse<CheckTemplate> check_manageTemplate_delete1(
            @RequestParam("TempID") int TempID
    )
    {
        CommonResponse<CheckTemplate> commonResponse =checkService.check_manageTemplate_delete(TempID);
        return commonResponse;
    }
    /**
        修改医技模板
        测试URL:http://127.0.0.1:9090/medical_technology_template/modify
        备注：Name,UsersID,CreationTime是不可以改的,然后Scope和RecordType不可以太大，会报错
        测试结果：正常
     */
    @PutMapping("/medical_technology_template/modify")
    public CommonResponse<CheckTemplate> check_manageTemplate_update1(
            @RequestParam("Name") String Name,
            @RequestParam("Scope") int Scope,
            @RequestParam("RecordType") int RecordType,
            @RequestParam("UsersID") int UsersID,
            @RequestParam("CreationTime") String CreationTime
            ){
        CommonResponse<CheckTemplate> commonResponse=checkService.check_manageTemplate_update(
                Name,Scope,RecordType,UsersID,CreationTime
        );
        return commonResponse;
    }
    /**
        查询所有医技模板
        测试URL:http://127.0.0.1:9090/medical_technology_template/allTemp
        备注：这个方法没有命名，url是我自己写的，注意和那个查询项目模板区分
        测试结果：正常
     */
    @GetMapping("/medical_technology_template/search/all")
    public CommonResponse<List<CheckTemplate>> check_manageTemplate_get1(

    ){
        CommonResponse<List<CheckTemplate>> commonResponse=checkService.check_manageTemplate_getList();
        return commonResponse;
    }
    /**
        新增医技项目
        测试URL:http://127.0.0.1:9090/medical_technology_template/add_pro
        备注：两个ID都不要太大
        测试结果：正常
     */
    @PostMapping("/medical_technology_template/add_pro")
    public CommonResponse<CheckRelation> check_manageRelation_add1(
            HttpServletRequest httpRequest

    ) {
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int CheckProjID=(int)jsonObject.get("CheckProjID");
        int CheckTempID=(int)jsonObject.get("CheckTempID");
        String Position =(String)jsonObject.get("Position");

        CommonResponse<CheckRelation> commonResponse =checkService.check_manageRelation_add(
                CheckProjID,CheckTempID,Position
        );
        return commonResponse;
    }
    /**
        删除医技项目
        测试URL:http://127.0.0.1:9090/medical_technology_template/del_pro/{check_pro_id}
        备注：这个方法传参数的形式我感觉是有点奇怪的
        测试结果：正常
     */
    @PostMapping("/medical_technology_template/del_pro")
    public CommonResponse<CheckRelation> check_manageRelation_delete(
            HttpServletRequest httpRequest)
    {
        JSONObject jsonObject = RequestToJsonObject.RequestToJsonObject(httpRequest);
        int CheckProjID = (int) jsonObject.get("CheckProjID");
        int CheckTempID = (int) jsonObject.get("CheckTempID");
        CommonResponse<CheckRelation> commonResponse =checkService.check_manageRelation_delete(CheckProjID,CheckTempID);
        return commonResponse;
    }
    /**
        通过模板ID查询医技详细
        测试URL:http://127.0.0.1:9090/medical_technology_template/search/{keywords}
        备注：这个方法是通过CheckTempID来查的
        测试结果：正常
     */
    @GetMapping("/medical_technology_template/search/{keywords}")
    public CommonResponse<CheckTemplate> check_manageRelation_get1(
            @PathVariable("keywords") String CheckTempName
    ){
        CommonResponse<CheckTemplate> commonResponse=checkService.check_manageRelation_getList(CheckTempName);
        return commonResponse;
    }
    /**
        通过模板ID查询医技详细
        测试URL:http://127.0.0.1:9090/medical_technology_template/search_detail
        备注：这个方法是通过CheckTempID来查的
        测试结果：正常
     */
    @GetMapping("/medical_technology_template/search_detail")
    public CommonResponse<List<FmedItem>> check_manageFmedItem_get1(
            @RequestParam("CheckTempID")int CheckTempID
    ){
        CommonResponse<List<FmedItem>> commonResponse=checkService.check_manageFmedItem_getList(CheckTempID);
        return commonResponse;
    }



}
